

package com.eshore.norm.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * 日期处理工具类
 *
 * @author link
 * @since 1.0.0
 */
public class DateUtils {
    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_MONTH_PATTERN = "yyyy-MM";
    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * 时间格式(yyyy-MM-ddTHH:mm:ss)
     */
    public final static String DATE_TIME_T_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";


    public final static String DATE_TIME_TZ_PATTERN = "yyyy-MM-dd'T'HH:mm:ss'Z'";


    public final static String DATE_TIME_TZS_PATTERN = "yyyy-MM-dd'T'HH:mm:ss:SSSZ";
    /**
     * 时间格式(yyyy-MM-dd HH:00:00)
     */
    public final static String DATE_HOUR_TIME_PATTERN = "yyyy-MM-dd HH:00:00";
    /**
     * 时间格式(yyyy-MM-dd 00:00:00)
     */
    public final static String DATE_BEGIN_TIME_PATTERN = "yyyy-MM-dd 00:00:00";
    /**
     * 时间格式(yyyy-MM-dd 00:00:00)
     */
    public final static String DATE_END_TIME_PATTERN = "yyyy-MM-dd 23:59:59";
    /**
     * 时间格式(mm:ss)
     */
    public final static String DATE_MINUTE_PATTERN = "mm:ss";

    /**
     * 日期格式化 日期格式为：yyyy-MM-dd
     *
     * @param date 日期
     * @return 返回yyyy-MM-dd格式日期
     */
    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    /**
     * 日期格式化 日期格式为：yyyy-MM-dd
     *
     * @param date    日期
     * @param pattern 格式，如：DateUtils.DATE_TIME_PATTERN
     * @return 返回yyyy-MM-dd格式日期
     */
    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    /**
     * 日期解析
     *
     * @param date    日期
     * @param pattern 格式，如：DateUtils.DATE_TIME_PATTERN
     * @return 返回Date
     */
    public static Date parse(String date, String pattern) {
        try {
            return new SimpleDateFormat(pattern).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
/*
    *//**
     * 字符串转换成日期
     *
     * @param strDate 日期字符串
     * @param pattern 日期的格式，如：DateUtils.DATE_TIME_PATTERN
     *//*
    public static Date stringToDate(String strDate, String pattern) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        }

        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        return fmt.parseLocalDateTime(strDate).toDate();
    }

    *//**
     * 根据周数，获取开始日期、结束日期
     *
     * @param week 周期  0本周，-1上周，-2上上周，1下周，2下下周
     * @return 返回date[0]开始日期、date[1]结束日期
     *//*
    public static Date[] getWeekStartAndEnd(int week) {
        DateTime dateTime = new DateTime();
        LocalDate date = new LocalDate(dateTime.plusWeeks(week));

        date = date.dayOfWeek().withMinimumValue();
        Date beginDate = date.toDate();
        Date endDate = date.plusDays(6).toDate();
        return new Date[]{beginDate, endDate};
    }

    *//**
     * 根据月数，获取开始日期、结束日期
     *
     * @param month 月份  0本月，-1上月，-2上上月，1下月，2下下月
     * @return 返回date[0]开始日期、date[1]结束日期
     *//*
    public static Date[] getMonthStartAndEnd(int month) {
        Date date = addDateMonths(new Date(), month);
        Date beginDate = getFirstDayDateOfMonth(date);
        Date endDate = getLastDayOfMonth(date);
        return new Date[]{beginDate, endDate};
    }

    *//**
     * 取当前周的周二和下周的周一
     *
     * @return
     *//*
    public static Date[] getThisTuesdayAndNextMonday() {
        LocalDate date = new LocalDate();
        date = date.dayOfWeek().withMinimumValue();
        Date beginDate = date.plusDays(1).toDate();
        Date endDate = date.plusDays(7).toDate();
        Calendar instance = Calendar.getInstance();
        instance.setTime(endDate);
        instance.set(Calendar.HOUR_OF_DAY, 1);
        endDate = instance.getTime();
        return new Date[]{beginDate, endDate};
    }


    public static Date[] getThisMonthSecondAndNextMonthFirst() {
        Date beginDate = getCurrentMonthSecondDay();
        Date endDate = getNextMonthFirstDay();
        return new Date[]{beginDate, endDate};
    }

    *//**
     * 对日期的【秒】进行加/减
     *
     * @param date    日期
     * @param seconds 秒数，负数为减
     * @return 加/减几秒后的日期
     *//*
    public static Date addDateSeconds(Date date, int seconds) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusSeconds(seconds).toDate();
    }

    *//**
     * 对日期的【分钟】进行加/减
     *
     * @param date    日期
     * @param minutes 分钟数，负数为减
     * @return 加/减几分钟后的日期
     *//*
    public static Date addDateMinutes(Date date, int minutes) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusMinutes(minutes).toDate();
    }

    *//**
     * 对日期的【小时】进行加/减
     *
     * @param date  日期
     * @param hours 小时数，负数为减
     * @return 加/减几小时后的日期
     *//*
    public static Date addDateHours(Date date, int hours) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusHours(hours).toDate();
    }

    *//**
     * 对日期的【天】进行加/减
     *
     * @param date 日期
     * @param days 天数，负数为减
     * @return 加/减几天后的日期
     *//*
    public static Date addDateDays(Date date, int days) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusDays(days).toDate();
    }

    *//**
     * 对日期的【周】进行加/减
     *
     * @param date  日期
     * @param weeks 周数，负数为减
     * @return 加/减几周后的日期
     *//*
    public static Date addDateWeeks(Date date, int weeks) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusWeeks(weeks).toDate();
    }

    *//**
     * 对日期的【月】进行加/减
     *
     * @param date   日期
     * @param months 月数，负数为减
     * @return 加/减几月后的日期
     *//*
    public static Date addDateMonths(Date date, int months) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusMonths(months).toDate();
    }

    *//**
     * 对日期的【年】进行加/减
     *
     * @param date  日期
     * @param years 年数，负数为减
     * @return 加/减几年后的日期
     *//*
    public static Date addDateYears(Date date, int years) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusYears(years).toDate();
    }*/

//    /**
//     * 对日期的【周】进行加/减
//     *
//     * @param weeks 周数，负数为减
//     * @return 加/减几周后的所有日期
//     */
//    public static List<Date> getWeeksList(int weeks) {
//        //周日期
//        Date week = addDateWeeks(new Date(), weeks);
//        //每周第一天
//        Calendar dBegin = Calendar.getInstance();
//        dBegin.setTime(getThisWeekMonday(week, Calendar.MONDAY));
//
//        return findDates(dBegin.getTime());
//    }

    /**
     * 获取指定日期所在周的周一
     *
     * @param date 日期
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            c.add(Calendar.DAY_OF_MONTH, -1);
        }
        c.add(Calendar.DATE, c.getFirstDayOfWeek() - c.get(Calendar.DAY_OF_WEEK) + 1);
        return c.getTime();
    }

    /**
     * 获取指定日期所在周的周一 0点数据 示例2022-10-31 00:00:00
     *
     * @param date 日期
     */
    public static Date getFirstDayOfWeekTo0Hour(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            c.add(Calendar.DAY_OF_MONTH, -1);
        }
        c.add(Calendar.DATE, c.getFirstDayOfWeek() - c.get(Calendar.DAY_OF_WEEK) + 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        return c.getTime();
    }

    /**
     * 得到指定时间周五日期
     * @return
     */
    public static Date getWeekFriday(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek() + 4);
        return cal.getTime();
    }

    /**
     * 得到指定时间周五日期最后时刻
     * @return
     */
    public static Date getFridayOfWeekTo23Hour(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 4);
        c.set(Calendar.HOUR_OF_DAY,23);
        c.set(Calendar.MINUTE,59);
        c.set(Calendar.SECOND,59);
        return c.getTime();
    }

    /**
     * 获取指定日期所在周的周六 0点数据
     * @return
     */
    public static Date getSaturdayOfWeekTo0Hour(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 5);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        return c.getTime();
    }

    /**
     * 获取指定日期所在周的周日
     *
     * @param date 日期
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // 如果是周日直接返回
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            return date;
        }
        System.out.println(c.get(Calendar.DAY_OF_WEEK));
        c.add(Calendar.DATE, 7 - c.get(Calendar.DAY_OF_WEEK) + 1);
        return c.getTime();
    }

    /**
     * 指定月份的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayDateOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int first = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, first);
        return cal.getTime();
    }

    /**
     * 指定月份的第一天0点
     *
     * @param date
     * @return
     */
    public static Date getFirstDayDateOfMonthTo0Hour(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int first = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, first);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND,0);
        return cal.getTime();
    }

    /**
     * 指定月份的最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, last);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }

    /**
     * 指定月份的最后一天第一时刻
     * @param date
     * @return
     */
    public static Date getLastDayOfMonthTo0Hour(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, last);
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        return cal.getTime();
    }

    /**
     * 指定月份的最后一天最后时刻
     * @param date
     * @return
     */
    public static Date getLastDayOfMonthTo23Hour(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, last);
        cal.set(Calendar.HOUR_OF_DAY,23);
        cal.set(Calendar.MINUTE,59);
        cal.set(Calendar.SECOND,59);
        cal.set(Calendar.MILLISECOND,0);
        return cal.getTime();
    }

    /**
     * 获取 某日期 所在的周一
     *
     * @return 加/减几周后的所有日期
     */
    public static Date getThisWeekMonday(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期在第几天
        cal.setFirstDayOfWeek(days);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }

    /**
     * 获取 某周 所有日期
     *
     * @return 加/减几周后的所有日期
     */
    public static List<Date> findDates(Date dBegin) {
        List lDate = new ArrayList();
        lDate.add(dBegin);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        // 测试此日期是否在指定日期之后
        for (int i = 1; i < Calendar.DAY_OF_WEEK; i++) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            lDate.add(calBegin.getTime());
        }
        return lDate;
    }

    /**
     * 获取 某月 所有日期
     *
     * @return 加/减几周后的所有日期
     */
    public static List<Date> getMonthFullDay(Date date) {
        List<Date> fullDayList = new ArrayList<>(32);
        // 获得当前日期对象
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 当月1号
        cal.set(Calendar.DAY_OF_MONTH, 1);

        int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int j = 1; j <= count; j++) {
            fullDayList.add(cal.getTime());
            cal.add(Calendar.DAY_OF_MONTH, 1);
        }
        return fullDayList;
    }

    public static String getDateFormateByTimeStamp(String time, String pattern) {
        if (StringUtils.isBlank(time)) {
            return null;
        }
        return format(new Date(Long.valueOf(time)), pattern);
    }

    /**
     * 获取当月有多少天
     *
     * @param date
     * @return 天数
     */
    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取当前月第一天
     */
    public static Date getCurrentMonthFirstDay() {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(Calendar.DAY_OF_MONTH, 1);
        calendar1.set(Calendar.HOUR, 0);
        calendar1.set(Calendar.MINUTE, 0);
        calendar1.set(Calendar.SECOND, 0);
        return calendar1.getTime();
    }

    /**
     * 获取当前月第二天
     */
    public static Date getCurrentMonthSecondDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 2);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取前一个月第一天
     */
    public static Date getLastMonthFirstDay() {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.MONTH, -1);
        calendar1.set(Calendar.DAY_OF_MONTH, 1);
        return calendar1.getTime();
    }

    /**
     * 获取下一个月第一天
     */
    public static Date getNextMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 1);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取前一个月最后一天
     */
    public static Date getLastMonthLastDay() {
        Calendar calendar2 = Calendar.getInstance();
        calendar2.set(Calendar.DAY_OF_MONTH, 0);
        return calendar2.getTime();
    }

    /**
     * UTC时间转北京时间
     *
     * @param dateStr
     * @return
     */
    public static Date formatUTCToBJDate(String dateStr) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        TimeZone utcZone = TimeZone.getTimeZone("UTC");
        sf.setTimeZone(utcZone);
        try {
            Date date = sf.parse(dateStr);
            return date;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 北京时间转UTC时间
     *
     * @param date
     * @return
     */
    public static String formatBJDateToUTC(Date date) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        TimeZone utcZone = TimeZone.getTimeZone("UTC");
        sf.setTimeZone(utcZone);
        try {
            return sf.format(date);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 10位数时间戳转日期
     *
     * @param timestamp
     * @return
     */
    public static String formatTimestamp(Long timestamp) {
        String date = Long.toString(timestamp * 1000L);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date(Long.parseLong(date)));
    }

    /**
     * 日期转10位数时间戳
     *
     * @param date
     * @return
     */
    public static Long DateToTimestamp(Date date) {
        return date.getTime() / 1000L;
    }

    public static void main(String[] args) {
        System.out.println(format(getCurrentMonthFirstDay()));
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            smdate = sdf.parse(sdf.format(smdate));
            bdate = sdf.parse(sdf.format(bdate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 获得当天零时零分零秒
     *
     * @return
     */
    public static Date initDateByDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获得昨天零时零分零秒
     *
     * @return
     */
    public static Date yesterdayDateByDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, calendar.get(Calendar.DAY_OF_MONTH) - 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


    /**
     * 根据日期获取n个月的工作日
     *
     * @param date
     * @return
     */
    public static List<Date> getWorkDates(Date date, Integer n) {
        List<Date> dates = new ArrayList<Date>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        for (int i = 0; i <= n; i++) {
            cal.set(Calendar.MONTH, month - 1);
            cal.set(Calendar.DATE, 1);
            while (cal.get(Calendar.YEAR) == year &&
                    cal.get(Calendar.MONTH) < month) {
                int day = cal.get(Calendar.DAY_OF_WEEK);
                if (!(day == Calendar.SUNDAY || day == Calendar.SATURDAY)) {
                    dates.add((Date) cal.getTime().clone());
                }
                cal.add(Calendar.DATE, 1);
            }
        }
        return dates;
    }

    /**
     * 获取今天的起始时间
     *
     * @return
     */
    public static Date getTodayStartTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    /**
     * 获取指定日期本周的工作日
     * @param date
     * @return
     */
    public static Date[] getWeekDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DAY_OF_WEEK, -1);
        }
        Date[] dates = new Date[5];  // new Date[7] 星期日
        for (int i = 0; i < 5; i++) {  // i < 7 星期日
            dates[i] = calendar.getTime();
            calendar.add(Calendar.DATE, 1);
        }
        return dates;
    }

    public static boolean isWeekDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return !(week == 6 || week == 0);
    }

    public static java.time.LocalDate dateToLocalDate(Date date){
        Instant instant =date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.toLocalDate();
    }

    /**
     * 传输时间的时候一定要注意  该方法是开区间的  很蛋疼  必须要前置时间往前加一天  后置时间往后延迟一天
     * 搬的代码没办法  没这实力改
     * 获取指定时间内星期几的所有日期
     * @param start 开始日期 2020-04-01
     * @param end   截止日期 2020-05-01
     * @param week  星期几  星期天传的是0 不是7!!!   1 -> 1   2 -> 2  ...   7 -> 0
     * @return  List<String> [2023-04-01, 2023-04-08, 2023-04-15, 2023-04-22, 2023-04-29]
     */
    public static List<String> getWeekInTimes(java.time.LocalDate start, java.time.LocalDate end, int week) {
        List<java.time.LocalDate> list = new ArrayList<>();
        long days = ChronoUnit.DAYS.between(start, end);
        Calendar startCalender = GregorianCalendar.from(start.atStartOfDay(ZoneId.systemDefault()));
        for (int i = 0; i < days - 1; i++) {
            startCalender.add(Calendar.DATE, 1);

            if (startCalender.get(Calendar.DAY_OF_WEEK) == week + 1) {// 1代表周日，7代表周六
                list.add(
                        LocalDateTime.ofInstant(
                                startCalender.toInstant(),
                                ZoneOffset.systemDefault()
                        ).toLocalDate()
                );
            }
        }
        List<String> dateStr = new ArrayList<>();
        list.forEach(t -> dateStr.add(t.toString()));
        return dateStr;
    }

    /**
     * 获取两个日期之间的所有月份 (年月)
     * @return：list
     */
    public static List<String> getMonthBetweenDate(Date start, Date end) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        // 声明保存日期集合
        List<String> list = new ArrayList<>();
            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (start.getTime() <= end.getTime()) {
                // 把日期添加到集合
                list.add(sdf.format(start));
                // 设置日期
                calendar.setTime(start);
                //把月数增加 1
                calendar.add(Calendar.MONTH, 1);
                // 获取增加后的日期
                start = calendar.getTime();
            }
        return list;
    }

}
